package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000119641669";

    // 商户私钥，您的PKCS8格式RSA2私钥
    public static String  merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCJo5rs9j52CBqTBQ3H5j6I8sBcYblSDY9OMXZJN+nKeHcd3byLI66KU/AIwWQKznnhDxucYZ0at27R3EPhdnhfrMBV7w2pWtv4xL4eklRgKdAHISUyKGoY5RyuZLtt3xjiIxsPKW7CIgTByzQB/loWYC+KKzlXb4WiDpushzARvgVG6EJvOZOfYsUi2Zs7Zq653U2Ki19tJbH7vHLedKRi2Q9lI1azJ2XOX0Ehdjc4PABn5oN53u8IsjkdYcVfLwsbdbItB0gf3bFJxyjYgBNcZOPRHpoFLpjJhWK7vRChWjAA4ijTDf6yA31sdEfvsGncZLwfaDhuyDZSVK+06HrfAgMBAAECggEACpsawwuugHXbZlzLVQ3+35qlC/lTyGjXFBVnofi3AS0LMLf3YVlch7g0FNiiFtsWNStu2zwCWR6tYWpxqJvyZpzvLTAAg35G2QKAC+cGrWpLZ2E8doL6ufRdo2qLrsm0JyjBWwe8SyVH7I718Plig7rhrunpvz0OtRAfGspH//Xgy0K5UcWpcEBh3rXriS4aY9ICgDxlgVEXu7wgYGdgJ9AXOHqr3Ies4gWpls8Gf6F1ehyvvIvO420WIT3jDkwhjehPRS0FAR7ywGnsUOBOtr3vUKRi/wB31oDI3pe+y6U0o8GEVlOuE8obSsxu7U3/f8U9fp1u1/8/PWMHJf5ZoQKBgQDsTytKOLrtgQf+bEpGys3usSb3zenTelPVUbxWgKdoXH/gjKTk+LsoYGdGiYlDVvvgobsckip6SevBEk1psZqlqBsw3G10DEHDvf+7ECzAyy0n+L7DilCcV+FeJ747KM8Q+9YGJmLVnVkYy9TrQYcg2d4sFJRZ9YoQZ6+AsJU/GQKBgQCVG6c+WNCb4gn3HsukYLKGJXxTZHsia4pU3bbnhHE/t0E0FsAgDS1pCnsxN3nqHRIJ5X1UY+0O5BYuUaS3hkh3MPPXuM+nw4Q1w+mpDYplXzOS9RTuihoqumeFrJniYLe3dKpk/SsW261hPF2TiyLbbFXVS97Yn0IKGspDi+dgtwKBgQCgx6TelQap/bagprFKxUWj96hZxiInnv1XEIuiF1rPqM/gyhCPMaS1bhIWbhGUXaR2S9PxNonsNktPS9Kb7RkZSO1nKzLVWFmOgsFyVyj768FHhvjUNlFubmSKGWRXAAcSuSX/fTKVwL8UHes6PZVhBpshRsdBBczwKHvChWKmUQKBgCs8xZZysqtBNJqQN4UG9T2pCRA1hu9IN+i8Ehm4cg6D2Uh9gGDK8WwQOgDfkV6t51kvTtR11XwH79amnhkJu2G0tEDNuolvTJs6oDsBTixvGsl2gAdbzJKvUu2US1s1qJBuKCr4rtIGCZS0qH3sTsNJ+unLAmfWZpP/iL9lhkapAoGAXGiI5ll9QS66CDEVDF97Vf5V/Rl/iEutx9haBZzJ7xB/bSRt8jH1rh/acUOk8bBXp62jEQbRg4ZIJsa+EKAOGHnD7ajmv1o3f2tMPWXvIqBSzalOw+VIxMylK7xBFZgfKGrhmP902K1PBsiDuEQjsQ963OBzDFxmeuaJXsb1PR4=";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public static String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAi5ULlQEScK7Qwtc1xDy8i/dfPXmYxDsXRgJOxsGhnPtovFONe18EEwRAtNuOFy805zrR+SfyZ2UdmE6iGzLPjxbizjaAvmeC3T7eOncjOD1qTz7e2/DPc8VjSbaA3UYeL1H09z23Ddeon+On0npOyexJXwwButrp1Awwqy8Pm+g9APBWF7sK4WKuVA2HW1xxcK/2/i8627UClhAa07o/26n4MyCEacGK+t1yYdRZV0SQAU/YABFBPn6xWRV6O5k9dC5smC0k+Hu4P+lJrxTOikA/yoU0xpru/KRuN/uJ0CJZQa7ia4RZfs2+R1l/HTW5ehju1OBQjZHLu56RZSR6BwIDAQAB";
    // 服务器异步通知页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //只要支付支付成功了，会每隔几秒发送一条消息，告知支付成功了，可以接收到这个消息以后，后台修改一下订单的这个成功页
    public static String notify_url = "http://mikbi8ou2s.51xd.pub/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //支付宝支付成功以后要跳转的页面地址
    public static String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
